package com.sjgtw.cloud.system.controller;

import com.sjgtw.cloud.common.core.constant.CacheConstants;
import com.sjgtw.cloud.common.core.exception.BaseException;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.core.utils.StringUtils;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.common.log.annotation.Log;
import com.sjgtw.cloud.common.log.enums.BusinessType;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.common.security.annotation.PreAuthorize;
import com.sjgtw.cloud.system.api.model.LoginUser;
import com.sjgtw.cloud.system.domain.SysDictData;
import com.sjgtw.cloud.system.service.ISysDictDataService;
import com.sjgtw.cloud.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title: 数据字典数据管理</p>
 * <p>Description: </p>
 * @author Admin
 * @date 2020-7-4
 */
@RestController
@RequestMapping("/dict/data")
public class SysDictDataController extends BaseController
{
	@Autowired
	private ISysDictDataService dictDataService;

	@Autowired
	private ISysDictTypeService dictTypeService;

	@Autowired
	private RedisService redisService;

	/**
	 * <p>Title: 字典数据列表</p>
	 * <p>Description: </p>
	 * @param dictData
	 * @return
	 */
	@PreAuthorize(hasPermi = "system:dict:list")
	@GetMapping("/list")
	public TableDataInfo list(SysDictData dictData) {
		startPage();
		List<SysDictData> list = dictDataService.selectDictDataList(dictData);
		return getDataTable(list);
	}

	/**
	 * <p>Title: 字典数据列表</p>
	 * <p>Description: </p>
	 * @param dictData
	 * @return
	 */
	@GetMapping("/dataInfoNoAuth")
	public AjaxResult dataInfoNoAuth(SysDictData dictData) {
		List<SysDictData> list = dictDataService.selectDictDataList(dictData);
		return AjaxResult.success(list);
	}
	
	/**
	 * <p>Title: 字典数据导出</p>
	 * <p>Description: </p>
	 * @param response
	 * @param dictType
	 * @throws IOException
	 */
	@Log(title = "字典数据", businessType = BusinessType.EXPORT)
	@PreAuthorize(hasPermi = "system:dict:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictData dictData) throws IOException
    {
//		List<SysDictData> list = dictDataService.selectDictDataList(dictData);
//        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
//        util.exportExcel(response, list, "字典数据");
        throw new BaseException("字典数据为敏感数据，不提供数据导出功能！");
    }

	/**
	 * 查询字典数据详细
	 */
	@PreAuthorize(hasPermi = "system:dict:query")
	@GetMapping(value = "/{dictCode}")
	public AjaxResult getInfo(@PathVariable Long dictCode) {
		return AjaxResult.success(dictDataService.selectDictDataById(dictCode));
	}

	/**
	 * 根据字典类型查询字典数据信息
	 */
	@GetMapping(value = "/type/{dictType}")
	public AjaxResult dictType(@PathVariable String dictType) {
		List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
		if (StringUtils.isNull(data)) {
			data = new ArrayList<SysDictData>();
		}
		return AjaxResult.success(data);
	}

	/**
	 * 新增字典类型
	 */
	@PreAuthorize(hasPermi = "system:dict:add")
	@Log(title = "字典数据", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@Validated @RequestBody SysDictData dict) {
		String token = SecurityUtils.getToken();
		String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
		LoginUser user = redisService.getCacheObject(userKey);
		dict.setCreateBy(user.getUsername());
		return toAjax(dictDataService.insertDictData(dict));
	}

	/**
	 * 修改保存字典类型
	 */
	@PreAuthorize(hasPermi = "system:dict:edit")
	@Log(title = "字典数据", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@Validated @RequestBody SysDictData dict) {
		String token = SecurityUtils.getToken();
		String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
		LoginUser user = redisService.getCacheObject(userKey);
		dict.setUpdateBy(user.getUsername());
		return toAjax(dictDataService.updateDictData(dict));
	}

	/**
	 * 删除字典类型
	 */
	@PreAuthorize(hasPermi = "system:dict:remove")
	@Log(title = "字典类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dictCodes}")
	public AjaxResult remove(@PathVariable Long[] dictCodes) {
		return toAjax(dictDataService.deleteDictDataByIds(dictCodes));
	}
}
